<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="搜索内容" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          @click="handleInit"
        >
          <svg-icon icon-class="init" />
          初始化</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID"  width="55" align="center" prop="id" />
      <el-table-column label="工具名称"  width="85" align="center" prop="tool_name" :show-overflow-tooltip="true" />
      <el-table-column label="工具描述" align="center" prop="tool_desc" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dicts.baseEnumEnabledDisabledOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="created_at" width="180" :formatter="formatterTimeYmdHm"/>
      <el-table-column label="更新时间" align="center" prop="updated_at" width="180" :formatter="formatterTimeYmdHm"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleInfo(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <AddAIToolDialog ref="AddAIToolDialogRef"/>

  </div>
</template>

<script>
import { delAITools, initAIAgent, initAITools, listAITools } from '@/api/ai'
import AddAIToolDialog from './add-ai-tool-dialog.vue'

export default {
  name: "AIToolsIndex",
  components:{
    AddAIToolDialog,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日志列表数据
      mainList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chat_id: undefined,
        searchValue: undefined,
      },
      // 详情对话框可见性
      detailVisible: false,
      // 当前查看的日志详情数据
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询日志列表 */
    getList() {
      this.loading = true;
      listAITools(this.queryParams).then(response => {
        this.mainList = response.list;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.mainList = [];
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    handleAdd({data,title='新增'}={}){
      this.$refs.AddAIToolDialogRef.show({
        form:this.sksUtils.mergeFromObject({},data,{
          payMode: 1,
          virtualChangeBitFlag: this.sksUtils.CHANGE_BIT_FLAG.add,

        }),
        dialogInfo:{
          title: title,
          submitCallbackSuccess:({resData}={})=>{
            this.getList()
          },
        }
      })
    },
    handleEdit(row){
      this.$refs.AddAIToolDialogRef.showByUniqueId({
        uniqueId: row.id,
        dialogInfo: {
          title: '修改',
          submitCallbackSuccess:({resData}={})=>{
            this.getList()
          },
        },
        virtualChangeBitFlag: this.sksUtils.CHANGE_BIT_FLAG.modify,
      })
    },
    handleInfo(row){
      this.$refs.AddAIToolDialogRef.showByUniqueId({
        uniqueId: row.id,
        dialogInfo: {
          title: '查看'
        },
        virtualChangeBitFlag: this.sksUtils.CHANGE_BIT_FLAG.none,
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(() => {
        return delAITools(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    handleInit(){
      this.$modal.confirm('是否初始化？').then(() => {
        return initAITools();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("初始化成功");
      }).catch(() => { });
    },
  }
};
</script>

<style scoped>

</style>
